Ext.onReady(function(){
	var fileAttachment = Ext.create('component.file.attachment',{
        objectId: '',
        imageType: "6",
        contextPath:contextPath
   });
	var changeRecordInfo = Ext.create("Ext.form.FormPanel", {
		autoWidth: true,
		autoHeight: true,
		fieldDefaults: {
            labelWidth: 120,
            labelAlign: "right",
            margin: "0 0 10 0"
        },
		items: [
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", name: "old_signCode", fieldLabel: "业务编号", width: 400},
					{xtype: "displayfield", name: "old_businessType", fieldLabel: "业务类型", widht: 300}
				]
			},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", name: "old_customerName", fieldLabel: "客户姓名", width: 400},
					{xtype: "displayfield", name: "old_hrrReceived", fieldLabel: "已收金额", width: 400, renderer: Ext.util.Format.numberRenderer('0,000.00')}
				]
			},
			{xtype: "displayfield", name: "old_didTime", fieldLabel: "业务日期", width: 400,
				format: 'Y-m-d',
		    	renderer : function(val) {
					if(val!=null&&val!=""){
		    			console.log(val);
						return Ext.Date.format(new Date(val), "Y-m-d");
					}
					return null;
				}
			},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", name: "old_roomName", fieldLabel: "原楼盘名称及房号", width: 400},
					{xtype: "displayfield", name: "old_calculateArea", fieldLabel: "原计价面积", width: 400, renderer: Ext.util.Format.numberRenderer('0,000.00')}
				]
			},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", name: "old_conversionPrice", fieldLabel: "原房屋单价", width: 400, renderer: Ext.util.Format.numberRenderer('0,000.00')},
					{xtype: "displayfield", name: "old_lumpsumPrice", fieldLabel: "原折前总价", width: 400, renderer: Ext.util.Format.numberRenderer('0,000.00')}
				]
			},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", fieldLabel: "原业务明细", value: 
						"<a href='javascript:parent.openMenu(\"0\",\"sig_"+relationId+"\",\"\",\"签约详情\",\"/customer/signed/toView?id="+relationId+"&returnUrl=,marketing,pin,index\")' style='cursor:pointer'>签约明细</a>"
						},
					{
						xtype:'button',
						text: "选择房间",
						margin: "0 0 0 20",
					    allowDepress: true,
					    enableToggle: true,
					    handler:function(){
					 		var selectWindow=Ext.create('component.estate.selectWindow',{
	           		 			callbackFun: function (record) {
		           		 	 		var result=Ext.JSON.decode(record);
	           		 	          	var form = changeRecordInfo.getForm();
	           		 	          	if(result.roomStatus == 1 || result.roomStatus == 3){
		           		 	          	form.findField("new_roomName").setValue(result.combinedName);
		           		 	          	form.findField("calculateArea").setValue(result.billingArea);
		           		 	          	form.findField("bfdUnitPrice").setValue(result.unitPrice);
		           		 	          	form.findField("bfdTotalPrice").setValue(result.totalPrice);
		           		 	          	form.findField("newRoomId").setValue(result.id);
	           		 	          	}else{
	           		 	          	    Ext.Msg.alert('提示', '此房源不可售!');
	           		 	          	}
	           		 	    	},
	           		 	        rStatus: '1',
	           		 	        customerId:customerId
	           		 	    });
	           		 	    selectWindow.show();  
		                }
					}
				]
			},
			{xtype: "textfield", name: "businessId", hidden: true, value: relationId},
			{xtype: "textfield", name: "newRoomId", hidden: true},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", name: "new_roomName", fieldLabel: "所换楼盘名称及房号", width: 400},
					{xtype: "textfield", name: "calculateArea", fieldLabel: "所换计价面积", width: 400, readOnly: true, renderer: Ext.util.Format.numberRenderer('0,000.00')}
				]
			},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "textfield", name: "bfdUnitPrice", fieldLabel: "所换房屋单价", width: 400, readOnly: true, renderer: Ext.util.Format.numberRenderer('0,000.00')},
					{xtype: "textfield", name: "bfdTotalPrice", fieldLabel: "所换折前总价", width: 400, readOnly: true, renderer: Ext.util.Format.numberRenderer('0,000.00')}
				]
			},
			{
				xtype : 'textarea', fieldLabel : '换房原因', width: 800, maxLength: 255,
				name: "remark", allowBlank:false, blankText:'不能为空'
			},
			{
				xtype: "fieldcontainer", layout: "hbox",
				items: [
					{xtype: "displayfield", name: "agentName", fieldLabel: "经纪人", width: 400},
					{xtype: "datefield", name: "applyDate", fieldLabel: "申请日期", width: 400,
						format: "Y-m-d", value: new Date(), readOnly: true
					}
				]
			},fileAttachment
		]
	});
	
	changeRecordInfo.getForm().load({
	    url: jypath + '/marketing/changeroom/getChangeRecordById',
	    params: {"id": relationId},
	    success: function(form, action){
	    	//已收签约金额,隐藏换房按钮
//	    	if(action.result.data.old_hrrReceived){
//	    		Ext.get("btn_save").hide();
//	    	}
	    },
	    failure: function(form, action) {
	    	Ext.get("btn_save").hide();
	        Ext.Msg.alert("加载失败", action.result.errorMessage);
	    }
	});
	
	var changeRecordPanel = Ext.create("Ext.panel.Panel", {
		renderTo:Ext.getBody(),
		title: "签约换房",
		items: [changeRecordInfo],
		bbar : [
			{
				xtype: "button",
				text : "返回",
				id: "btn_back",
				name: "btn_back",
				icon : jypath + '/resources/ExtJSicons/shape_move_backwards.png',
				listeners: {
					click: function(){
						 var tabKey = "cgr_"+relationId;
				         parent.TabControlRemove(tabKey);
					}
				}
			},{
				xtype: "button",
				text : "提交审核",
				id: "btn_save",
				name: "btn_save",
				icon : jypath + '/resources/ExtJSicons/database_add.png',
				listeners:{
					click: function(){
						var form = changeRecordInfo.getForm();
						console.log(form.getValues());
						//return;
					    if(form.isValid()){
					    	// 发送请求
					    	var newRoomId = form.findField("newRoomId").getValue();
					    	if(!newRoomId){
					    		Ext.Msg.alert('失败', "请选择换房房间");
					    		return false;
					    	}
					    	var waiting = Ext.Msg.wait('正在处理，请稍等...','','');
					    	var fileArrs=fileAttachment.getFileArrs();
					    	var params=form.getValues();
					    	params.fileArr=fileArrs;
							Ext.Ajax.request({
								method: "POST",
								url: jypath + "/marketing/changeroom/changeRoomSubmit",
								params: params,
								success: function(response, options){
									if(waiting != null){
										waiting.hide();
									}
									var jsonResult = Ext.decode(response.responseText);
									if(jsonResult.res == 1){
										   Ext.MessageBox.alert("系统提示", "提交成功! ", function(but) {  
											   var tabKey = "cgr_"+relationId;
										       parent.TabControlRemove(tabKey);
		    				                }); 
									}else{
										Ext.Msg.alert('失败', jsonResult.resMsg);
									}
								},
								failure: function(response, options){
									Ext.Msg.alert('错误', '请求超时.');
								}
							});
						}
					}
				}
			}
		]
	});
});